﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RTUParameterConfig.Class;
using System.Collections;

namespace RTUParameterConfig.Controls
{
    public partial class WaterTemperature : UserControl
    {
        public int intIndex;
        public int DeviceSelectNo;//设备选择序号

        public WaterTemperature()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 初始化参数
        /// </summary>
        /// <param name="type">设备类型</param>
        /// <param name="index">设备标识</param>
        /// <param name="intDeviceSelectNo">设备选择序号</param>
        public void InitValue(int type, int index, int intDeviceSelectNo)
        {
            gc.Text = Common.WaterTemperatureList[intDeviceSelectNo].ToString();
            DeviceSelectNo = intDeviceSelectNo;
            intIndex = index;
            this.Tag = type;
            this.TabIndex = (type + 1) * 10 + index;
        }

        /// <summary>
        /// 设置水温配置
        /// </summary>
        public void SetValue()
        {
            nud1.Value = ParameterConfig.rtuConfigInfo.WaterTemperatureAddr;
            ccbZz.SelectedIndex = GetItemIndex(ParameterConfig.rtuConfigInfo.WaterTemperatureSources);
        }

        /// <summary>
        /// 获取水温配置
        /// </summary>
        public void GetValue()
        {
            ParameterConfig.rtuConfigInfo.WaterTemperature_Enable |= 0x0001 << 0;
            ParameterConfig.rtuConfigInfo.WaterTemperatureAddr = Convert.ToByte(nud1.Value);
            ParameterConfig.rtuConfigInfo.WaterTemperatureSources = Convert.ToByte((ccbZz.SelectedItem as ListItem).Value);
        }

        public void BindList(ArrayList listDevice)
        {
            ListItem item = null;
            int intValue = -1;
            if (ccbZz.SelectedItem != null)
            {
                intValue = (ccbZz.SelectedItem as ListItem).Value;
            }
            ccbZz.Properties.Items.Clear();

            item = new ListItem("独立水温计", 0);
            ccbZz.Properties.Items.Add(item);

            if (listDevice.Contains("1"))
            {
                item = new ListItem("水位计1", 1);
                ccbZz.Properties.Items.Add(item);
            }

            if (listDevice.Contains("2"))
            {
                item = new ListItem("水位计2", 2);
                ccbZz.Properties.Items.Add(item);
            }

            if (listDevice.Contains("3"))
            {
                item = new ListItem("流量计1", 3);
                ccbZz.Properties.Items.Add(item);
            }

            if (listDevice.Contains("4"))
            {
                item = new ListItem("流量计2", 4);
                ccbZz.Properties.Items.Add(item);
            }

            if (listDevice.Contains("5"))
            {
                item = new ListItem("在线PH传感器", 5);
                ccbZz.Properties.Items.Add(item);
            }

            if (listDevice.Contains("6"))
            {
                item = new ListItem("在线溶解氧传感器", 6);
                ccbZz.Properties.Items.Add(item);
            }

            if (listDevice.Contains("7"))
            {
                item = new ListItem("在线电导率传感器", 7);
                ccbZz.Properties.Items.Add(item);
            }

            if (listDevice.Contains("8"))
            {
                item = new ListItem("在线浊度传感器(1000NTU)", 8);
                ccbZz.Properties.Items.Add(item);
            }

            if (listDevice.Contains("9"))
            {
                item = new ListItem("在线浊度传感器(4000NTU)", 8);
                ccbZz.Properties.Items.Add(item);
            }

            if (listDevice.Contains("10"))
            {
                item = new ListItem("在线COD传感器", 10);
                ccbZz.Properties.Items.Add(item);
            }

            if (listDevice.Contains("11"))
            {
                item = new ListItem("在线氨氮传感器", 11);
                ccbZz.Properties.Items.Add(item);
            }

            if (listDevice.Contains("12"))
            {
                item = new ListItem("在线余氯传感器", 12);
                ccbZz.Properties.Items.Add(item);
            }

            if (listDevice.Contains("13"))
            {
                item = new ListItem("在线多参数水质传感器", 13);
                ccbZz.Properties.Items.Add(item);
            }

            if (listDevice.Contains("14"))
            {
                item = new ListItem("在线ORP传感器", 14);
                ccbZz.Properties.Items.Add(item);
            }

            if (ccbZz.Properties.Items.Count > 0)
            {
                if (intValue != -1)
                {
                    for (int i = 0; i < ccbZz.Properties.Items.Count; i++)
                    {
                        if (intValue == (ccbZz.Properties.Items[i] as ListItem).Value)
                        {
                            ccbZz.SelectedIndex = i;
                            return;
                        }
                    }
                    ccbZz.SelectedIndex = 0;
                }
                else
                {
                    ccbZz.SelectedIndex = 0;
                }
            }
            else
            {
                ccbZz.SelectedIndex = -1;
            }
        }

        public int GetItemIndex(int intValue)
        {
            for (int i = 0; i < ccbZz.Properties.Items.Count; i++)
            {
                if ((ccbZz.Properties.Items[i] as ListItem).Value == intValue)
                {
                    return i;
                }
            }
            return -1;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("确定要删除设备?", "删除设备", MessageBoxButtons.OKCancel);
            if (dr == DialogResult.OK)
            {
                Control c = this.Parent;
                c.Controls.Remove(this);
            }
        }

        private void ccbZz_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ccbZz.SelectedIndex == 0)
            {
                Common.ShowMessage("选择独立水温计请设置通讯地址");
            }
        }

        private void btnDebug_Click(object sender, EventArgs e)
        {
            MessageInfo.SendMessage(ParameterConfig.mainFormName, ParameterConfig.debugMsg, 9, intIndex + 1);
        }
    }

    public class ListItem : Object
    {
        public string Text { get; set; }

        public int Value { get; set; }

        public ListItem(string text, int value)
        {
            this.Text = text;
            this.Value = value;
        }

        public override string ToString()
        {
            return this.Text;
        }
    }
}
